package com.ezjie.ielts.module_main;

import android.app.Dialog;

import java.io.File;

import android.app.Dialog;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.Html;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.ezjie.ielts.R;
import com.ezjie.ielts.core.base.BaseBean;
import com.ezjie.ielts.core.base.BaseFragmentActivity;
import com.ezjie.ielts.core.http.HttpRequestAbstractCallBack;
import com.ezjie.ielts.core.spfs.SharedPrefHelper;
import com.ezjie.ielts.model.HomeDailyMessage;
import com.ezjie.ielts.model.HomeData;
import com.ezjie.ielts.model.HomeInfo;
import com.ezjie.ielts.model.HomeUserDaily;
import com.ezjie.ielts.model.HomeWord;
import com.ezjie.ielts.model.UpdateDetail;
import com.ezjie.ielts.model.UpdateInfo;
import com.ezjie.ielts.model.WordNewCheckData;
import com.ezjie.ielts.model.WordNewCheckResponse;
import com.ezjie.ielts.module_listen.ListenExamActivity;
import com.ezjie.ielts.module_read.ReadMainActivity;
import com.ezjie.ielts.module_set.SettingActivity;
import com.ezjie.ielts.module_word.OfflineManageActivity;
import com.ezjie.ielts.module_word.WordPracticeActivity;
import com.ezjie.ielts.service.UpdateCompleteService;
import com.ezjie.ielts.task.CheckUpdateTask;
import com.ezjie.ielts.task.HomeTask;
import com.ezjie.ielts.task.WordTask;
import com.ezjie.ielts.util.AppManager;
import com.ezjie.ielts.util.AppUtil;
import com.ezjie.ielts.util.DateUtil;
import com.ezjie.ielts.util.LogUtils;
import com.ezjie.ielts.util.NetworkUtil;
import com.ezjie.ielts.util.UmengPages;
import com.ezjie.ielts.util.WordHomeDataUtil;
import com.ezjie.login.LoginActivity;
import com.ezjie.login.db.UserInfoDB;
import com.ezjie.login.model.UserDetail;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

/**
 * 首页2次改版
 * 
 * @author renyangyang
 *
 */
@ContentView(R.layout.activity_navigation)
public class NavigationActivity extends BaseFragmentActivity {

	private static final String TAG = NavigationActivity.class.getSimpleName();
	private int intentAnimationType;
	private static final int READ_ANIMATION_TYPE = 1;
	private static final int LISTEN_ANIMATION_TYPE = 2;
	private static final int VOCABULARY_ANIMATION_TYPE = 3;

	@ViewInject(R.id.rl_main2)
	private RelativeLayout rl_main2;
	@ViewInject(R.id.iv_bg)
	private ImageView iv_bg;
	@ViewInject(R.id.iv_backLoad)
	private ImageView iv_backLoad;
	@ViewInject(R.id.iv_gerenzhuye)
	private ImageView iv_gerenzhuye;// 个人主页按钮

	@ViewInject(R.id.tv_day)
	private TextView tv_day;
	@ViewInject(R.id.tv_monthAndYear)
	private TextView tv_monthAndYear;
	@ViewInject(R.id.tv_everyDayWarn)
	private TextView tv_everyDayWarn;
	@ViewInject(R.id.tv_zan)
	private TextView tv_zan;
	@ViewInject(R.id.iv_plus_1)
	private ImageView iv_plus_1;// 赞+1
	@ViewInject(R.id.ll_zan)
	private LinearLayout ll_zan;
	
	@ViewInject(R.id.rl_vocabulary)
	private RelativeLayout rl_vocabulary;// 词汇layout
	@ViewInject(R.id.vocabulary_line)
	private View vocabulary_line;
	@ViewInject(R.id.tv_vocabulary_progress)
	private TextView tv_vocabulary_progress;// 词汇学习进度

	@ViewInject(R.id.rl_listening)
	private RelativeLayout rl_listening;// 听力layout
	@ViewInject(R.id.listen_line)
	private View listen_line;
	@ViewInject(R.id.tv_listen_progress)
	private TextView tv_listen_progress;// 听力学习进度

	@ViewInject(R.id.rl_reading)
	private RelativeLayout rl_reading;// 阅读layout
//	@ViewInject(R.id.read_line)
//	private View read_line;
	@ViewInject(R.id.tv_have_seat_month)
	private TextView tv_have_seat_month;//
	@ViewInject(R.id.tv_read_progress)
	private TextView tv_read_progress;// 阅读学习进度

	@ViewInject(R.id.rl_dayAndWarn)
	private RelativeLayout rl_dayAndWarn;

	@ViewInject(R.id.rl_vocabulary_clickAnimation)
	private RelativeLayout rl_vocabulary_clickAnimation;
	@ViewInject(R.id.rl_read_clickAnimation)
	private RelativeLayout rl_read_clickAnimation;
	@ViewInject(R.id.rl_listen_clickAnimation)
	private RelativeLayout rl_listen_clickAnimation;
	@ViewInject(R.id.ll_white)
	private LinearLayout ll_white;

	private UserInfoDB userInfoDB;
	private UserDetail userDetail;
	private HomeTask homeTask;
	private WordTask mWordTask;
	private CheckUpdateTask updateTask;// 升级task

	private Context mContext;
	public static Intent serviceIntent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);

		userInfoDB = new UserInfoDB(this);
		homeTask = new HomeTask(this);
		mWordTask = new WordTask(this);
		updateTask = new CheckUpdateTask(this);

		mContext = this;
		init();
	}

	private void init() {
		ll_zan.setOnClickListener(this);
		iv_gerenzhuye.setOnClickListener(this);
		rl_vocabulary.setOnClickListener(this);
		rl_listening.setOnClickListener(this);
		rl_reading.setOnClickListener(this);

		// 读取默认值
		tv_day.setText(DateUtil.getCurrentDay());
		tv_monthAndYear.setText(DateUtil.getCurrentMonthAndYearEn());
		
		getWordIsUpdate();
		checkUpdata();
	}

	private void checkUpdata() {
		updateTask.checkUpdate(new HttpRequestAbstractCallBack(this,false) {
			@Override
			public void onStartCallBack() {
				// TODO Auto-generated method stub
				super.onStartCallBack();
				AppUtil.showProgressDialog(mContext);
			}

			@Override
			public void onSuccessCallBack(String result) {
				// TODO Auto-generated method stub
				super.onSuccessCallBack(result);
				AppUtil.dismissProgressDialog();
				LogUtils.d(result);
				try {
					UpdateInfo info = JSON.parseObject(result, UpdateInfo.class);
					if (info != null) {
						// 把当前用户登录状态设置为0
						UpdateDetail detail = info.data.update;
						if(detail != null){
							SharedPrefHelper.getInstance().putUpdateData("updateData", JSON.toJSONString(detail));
							if (detail.has_update.equals("1")) {
								// 需要更新
								showUpdateDialog(detail);
							}
						}
					}
				} catch (Exception e) {
					LogUtils.exception(e);
				}

			}

			@Override
			public void onFailureCallBack(HttpException error, String msg) {
				// TODO Auto-generated method stub
				super.onFailureCallBack(error, msg);
				AppUtil.dismissProgressDialog();
				AppUtil.showToast(NavigationActivity.this, R.string.load_net_data_failure);
			}
		});
	}

	private void showUpdateDialog(final UpdateDetail detail) {
		View view = LayoutInflater.from(NavigationActivity.this).inflate(R.layout.layout_update_msg_dialog, null);

		final Dialog dialog = new Dialog(NavigationActivity.this, R.style.customDialog);
		dialog.setCanceledOnTouchOutside(false);
		dialog.setContentView(view);
		dialog.show();

		TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
		tv_content.setText(Html.fromHtml(detail.content));
		LinearLayout ll_layout1 = (LinearLayout) view.findViewById(R.id.ll_layout1);
		LinearLayout ll_layout2 = (LinearLayout) view.findViewById(R.id.ll_layout2);
		String is_force = detail.is_force;
		if ("1".equals(is_force)) {
			// 强制更新
			ll_layout2.setVisibility(View.VISIBLE);
			ll_layout1.setVisibility(View.GONE);
			dialog.setCancelable(false);
		} else {
			ll_layout2.setVisibility(View.GONE);
			ll_layout1.setVisibility(View.VISIBLE);
			dialog.setCancelable(true);
		}

		Button btn_yes = (Button) view.findViewById(R.id.btn_yes);
		Button btn_no = (Button) view.findViewById(R.id.btn_no);
		Button btn_know = (Button) view.findViewById(R.id.btn_know);

		btn_no.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (dialog != null && dialog.isShowing()) {
					dialog.cancel();
				}
			}
		});
		btn_yes.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (dialog != null && dialog.isShowing()) {
					dialog.cancel();
				}
				startDownloadApk(detail);
			}
		});
		btn_know.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (dialog != null && dialog.isShowing()) {
					dialog.cancel();
				}
				startDownloadApk(detail);
			}
		});
	}

	private void startDownloadApk(UpdateDetail detail) {
		if (!NetworkUtil.isNetworkAvailable(NavigationActivity.this)) {
			AppUtil.showToast(this, R.string.no_network);
			return;
		}
		if (detail == null) {
			return;
		}
		// 判断是否有外部存储卡
		String status = Environment.getExternalStorageState();
		if (!status.equals(Environment.MEDIA_MOUNTED)) {
			// 在 vivo X3L报很多次错，Destination must be on external storage
			AppUtil.showToast(this, R.string.cannot_user_sdcard);
			return;
		}
		AppUtil.showToast(this, R.string.update_downing);
		serviceIntent = new Intent(this, UpdateCompleteService.class);
		serviceIntent.putExtra("update", detail);
		startService(serviceIntent);
	}

	@Override
	public void onClickEvent(View v) {
		switch (v.getId()) {
		case R.id.rl_vocabulary:
			intentAnimationType = VOCABULARY_ANIMATION_TYPE;
			intentAnimation();
			break;
		case R.id.rl_listening:
			intentAnimationType = LISTEN_ANIMATION_TYPE;
			intentAnimation();
			break;
		case R.id.rl_reading:
			intentAnimationType = READ_ANIMATION_TYPE;
			intentAnimation();
			break;
		case R.id.iv_gerenzhuye:
			// 友盟统计
			MobclickAgent.onEvent(mContext, "home_setting");
			// 判断用户是否登录

			if (userInfoDB != null) {
				userDetail = userInfoDB.queryLoginUser();
				if (userDetail != null ) {
					// 已登录
					System.out.println((userDetail != null)+""+userDetail.islogin+"");

				} else {
					startActivity(new Intent(this, LoginActivity.class));
					System.out.println((userDetail != null)+""+userDetail.islogin+"");

					return;
				}
			}
			startActivity(new Intent(this, SettingActivity.class));
			break;
		case R.id.ll_zan:
			// 判断用户是否登录
			if (userInfoDB != null) {
				userDetail = userInfoDB.queryLoginUser();
				if (userDetail != null && userDetail.islogin == 1) {
					System.out.println((userDetail != null)+""+userDetail.islogin+"");
					// 已登录
				} else {
//					System.out.println((userDetail != null)+""+userDetail.islogin+"");
					startActivity(new Intent(this, LoginActivity.class));
					return;
				}
			}
			// 赞点击
			zanClick();
			break;
		default:
			break;
		}
	}

	private void zanClick() {
		// 先判断今天是否已经点击过
		if (userDetail != null && userDetail.islogin == 1) {
			String zanTime = SharedPrefHelper.getInstance().getZanTime(userDetail.uid);
			String currentTime = DateUtil.getCurrentTimeYYYYMMDD();
			if (zanTime.equals(currentTime)) {
				AppUtil.showToast(this, R.string.main_zan_tip);
			} else {
				if (!NetworkUtil.isNetworkAvailable(this)) {
					AppUtil.showToast(this, R.string.no_network);
					return;
				}
				SharedPrefHelper.getInstance().setZanTime(userDetail.uid);
				// 请求赞接口
				homeTask.homeZan(new HttpRequestAbstractCallBack(this,false) {
					@Override
					public void onStartCallBack() {
						// TODO Auto-generated method stub
						super.onStartCallBack();
					}

					@Override
					public void onSuccessCallBack(String result) {
						// TODO Auto-generated method stub
						super.onSuccessCallBack(result);
						LogUtils.d(result);
						// 友盟统计
						MobclickAgent.onEvent(mContext, "home_zan");

						try {
							BaseBean baseBean = JSON.parseObject(result, BaseBean.class);
							if (baseBean != null && baseBean.getStatus_code() == 200) {
								// 动画
								TranslateAnimation tAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,
										Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
										Animation.RELATIVE_TO_SELF, -1f);
								AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);

								// 动画集
								AnimationSet set1 = new AnimationSet(true);
								set1.addAnimation(tAnimation);
								set1.addAnimation(alphaAnimation);
								set1.setDuration(300);
								iv_plus_1.startAnimation(set1);

								String zanStr = tv_zan.getText().toString();
								int zanNum = Integer.parseInt(zanStr);
								tv_zan.setText(zanNum + 1 + "");

							}
						} catch (Exception e) {
							// TODO: handle exception
						}
					}

					@Override
					public void onFailureCallBack(HttpException error, String msg) {
						// TODO Auto-generated method stub
						super.onFailureCallBack(error, msg);
						AppUtil.showToast(NavigationActivity.this, R.string.load_net_data_failure);
					}
				});
			}

		}
	}

	// 跳转动画，先移动再放大
	private void intentAnimation() {
		TranslateAnimation tAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1f,
				Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
		ScaleAnimation sAnimation = new ScaleAnimation(1f, 1.5f, 1f, 1f);

		// 动画集
		AnimationSet set = new AnimationSet(true);
		set.addAnimation(tAnimation);
		set.addAnimation(sAnimation);
		set.setDuration(300);
		set.setFillAfter(true);

		switch (intentAnimationType) {
		case READ_ANIMATION_TYPE:
			rl_read_clickAnimation.startAnimation(set);
			break;
		case LISTEN_ANIMATION_TYPE:
			rl_listen_clickAnimation.startAnimation(set);
			break;
		case VOCABULARY_ANIMATION_TYPE:
			rl_vocabulary_clickAnimation.startAnimation(set);
			break;
		default:
			break;
		}

		set.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				switch (intentAnimationType) {
				case READ_ANIMATION_TYPE:
					rl_read_clickAnimation.setVisibility(View.VISIBLE);
					break;
				case LISTEN_ANIMATION_TYPE:
					rl_listen_clickAnimation.setVisibility(View.VISIBLE);
					break;
				case VOCABULARY_ANIMATION_TYPE:
					rl_vocabulary_clickAnimation.setVisibility(View.VISIBLE);
					break;

				default:
					break;
				}

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// 开始放大
				ScaleAnimation sAnimation = new ScaleAnimation(1.5f, 1.5f, 1f, 30f, Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);
				// ScaleAnimation sAnimation1 = new ScaleAnimation(1.5f, 1.5f,
				// 1f, 10f);

				AnimationSet set1 = new AnimationSet(true);
				set1.addAnimation(sAnimation);
				// set1.addAnimation(sAnimation1);
				set1.setDuration(300);
				// set1.setFillAfter(true);

				switch (intentAnimationType) {
				case READ_ANIMATION_TYPE:
					rl_read_clickAnimation.startAnimation(set1);
					break;
				case LISTEN_ANIMATION_TYPE:
					rl_listen_clickAnimation.startAnimation(set1);
					break;
				case VOCABULARY_ANIMATION_TYPE:
					rl_vocabulary_clickAnimation.startAnimation(set1);
					break;

				default:
					break;
				}
				set1.setAnimationListener(new AnimationListener() {
					@Override
					public void onAnimationStart(Animation animation) {

						AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1.0f);

						// 动画集
						AnimationSet set2 = new AnimationSet(true);
						set2.addAnimation(alphaAnimation);
						set2.setDuration(200);
						ll_white.startAnimation(set2);
						// jijing_line.startAnimation(alphaAnimation);
						set2.setAnimationListener(new AnimationListener() {
							@Override
							public void onAnimationStart(Animation animation) {
								ll_white.setVisibility(View.VISIBLE);
								rl_read_clickAnimation.setVisibility(View.INVISIBLE);
								rl_listen_clickAnimation.setVisibility(View.INVISIBLE);
							}

							@Override
							public void onAnimationRepeat(Animation animation) {
							}

							@Override
							public void onAnimationEnd(Animation animation) {
								switch (intentAnimationType) {
								case READ_ANIMATION_TYPE:
									// 友盟统计
									MobclickAgent.onEvent(mContext, "home_readPractice");
									startActivity(new Intent(NavigationActivity.this, ReadMainActivity.class));
									overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
									break;
								case LISTEN_ANIMATION_TYPE:
									// 友盟统计
									MobclickAgent.onEvent(mContext, "home_listenPractice");
									startActivity(new Intent(NavigationActivity.this, ListenExamActivity.class));
									overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
									break;
								case VOCABULARY_ANIMATION_TYPE:
									// 友盟统计
									MobclickAgent.onEvent(mContext, "home_vocabulary_practice");
									if (userInfoDB.queryLoginUser().islogin == 0) {
										startActivity(new Intent(mContext, LoginActivity.class));
										return;
									}
									startActivity(new Intent(mContext, WordPracticeActivity.class));
									overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
									break;
								default:
									break;
								}
							}
						});

					}

					@Override
					public void onAnimationRepeat(Animation animation) {
					}

					@Override
					public void onAnimationEnd(Animation animation) {
					}
				});
			}
		});

	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(UmengPages.PAGE_MAIN);
		// 开始动画
		startAnimation();

		refreshProcessData();
	}

	private void refreshProcessData() {
		homeTask.homeData(new HttpRequestAbstractCallBack(this,false) {
			@Override
			public void onStartCallBack() {
				// TODO Auto-generated method stub
				super.onStartCallBack();
			}

			@Override
			public void onSuccessCallBack(String result) {
				// TODO Auto-generated method stub
				super.onSuccessCallBack(result);
				LogUtils.d(result);
				try {
					HomeInfo homeInfo = JSON.parseObject(result, HomeInfo.class);
					if (homeInfo != null) {
						HomeData data = homeInfo.getData();
						if (data != null) {
							// 阅读和听力
							String progress = getResources().getString(R.string.home_practice_progress);
							String progress1 = getResources().getString(R.string.home_practice_progress_vc);
							String listening_process = data.getListening_question_process();
							String reading_process = data.getReading_question_process();
							tv_listen_progress.setText(String.format(progress, listening_process));
							tv_read_progress.setText(String.format(progress, reading_process));
							if(!data.getWord().equals("")){
								HomeWord word = JSON.parseObject(data.getWord(), HomeWord.class);
								tv_vocabulary_progress.setText(String.format(progress1, word.getComplete()));
							}else{
								tv_vocabulary_progress.setText(String.format(progress1, 0));
							}
							// 日期和名句
							HomeDailyMessage daily_message = data.getDaily_message();
							if (daily_message != null) {
								tv_zan.setText(daily_message.getPraise_num() + "");
								tv_everyDayWarn.setText(Html.fromHtml(daily_message.getMessage()));
								String today = daily_message.getToday();
								tv_day.setText(DateUtil.getCurrentDayByHttp(today));
								tv_monthAndYear.setText(DateUtil.getCurrentMonthAndYearEnByHttp(today));
							}
							// 保存我的目标
							HomeUserDaily user_daily = data.getUser_daily();
							if (userInfoDB != null) {
								userDetail = userInfoDB.queryLoginUser();
							}
							if (user_daily != null && userDetail != null) {
//								SharedPrefHelper.getInstance().setDailyNum(userDetail.uid, user_daily.getDaily_num());
								SharedPrefHelper.getInstance().setDailyNum(userDetail.uid, user_daily.getDaily_question_num());
								SharedPrefHelper.getInstance().setIsRand(userDetail.uid, user_daily.getIs_rand());
//								SharedPrefHelper.getInstance().setFinishNum(user_daily.getFinish_num());
								SharedPrefHelper.getInstance().setFinishNum(userDetail.uid, user_daily.getFinish_question_num());
							}
						}
					}
				} catch (Exception e) {
					LogUtils.exception(e);
				}
			}

			@Override
			public void onFailureCallBack(HttpException error, String msg) {
				// TODO Auto-generated method stub
				super.onFailureCallBack(error, msg);
				AppUtil.showToast(NavigationActivity.this, R.string.load_net_data_failure);
			}
		});

	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(UmengPages.PAGE_MAIN);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(null != serviceIntent){
			stopService(serviceIntent);
		}
	}

	private long exitTime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
			if ((System.currentTimeMillis() - exitTime) > 2000) {
				AppUtil.showToast(this, R.string.back_again);
				exitTime = System.currentTimeMillis();
			} else {
				MobclickAgent.onKillProcess(this);
				AppManager.getAppManager().exitApplication(this);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	/*********************** 所有动画 *********************************/
	// 开始每个块的动画
	private int duration = 250;

	private void startAnimation() {
		// 1) 抢考位 从下往上，从无到有
		ll_white.setVisibility(View.INVISIBLE);
		rl_vocabulary_clickAnimation.setVisibility(View.INVISIBLE);
		rl_read_clickAnimation.setVisibility(View.INVISIBLE);
		rl_listen_clickAnimation.setVisibility(View.INVISIBLE);
		rl_vocabulary.setVisibility(View.INVISIBLE);
		rl_listening.setVisibility(View.INVISIBLE);
		rl_reading.setVisibility(View.INVISIBLE);
		vocabulary_line.setVisibility(View.INVISIBLE);
		listen_line.setVisibility(View.INVISIBLE);
//		read_line.setVisibility(View.INVISIBLE);
		rl_dayAndWarn.setVisibility(View.INVISIBLE);
		iv_gerenzhuye.setVisibility(View.INVISIBLE);
		startFirstAnimation();
	}
	
	private void startFirstAnimation(){
		TranslateAnimation tAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,
				Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0f);
		AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);

		// 动画集
		AnimationSet set1 = new AnimationSet(true);
		set1.addAnimation(tAnimation);
		set1.addAnimation(alphaAnimation);
		set1.setDuration(duration);
		set1.setInterpolator(new DecelerateInterpolator());
		rl_vocabulary.startAnimation(set1);
		// jijing_line.startAnimation(alphaAnimation);
		set1.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				rl_vocabulary.setVisibility(View.VISIBLE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				vocabulary_line.setVisibility(View.VISIBLE);
				startGreAnimation();
			}
		});
	}

	private void startGreAnimation() {
		TranslateAnimation tAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,
				Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0f);
		AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);
		
		// 动画集
		AnimationSet set1 = new AnimationSet(true);
		set1.addAnimation(tAnimation);
		set1.addAnimation(alphaAnimation);
		set1.setDuration(duration);
		set1.setInterpolator(new DecelerateInterpolator());
		rl_listening.startAnimation(set1);
		
		set1.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				rl_listening.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				listen_line.setVisibility(View.VISIBLE);
				startSeatAnimation();
			}
		});
	}
	
	private void startSeatAnimation() {
		TranslateAnimation tAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,
				Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0f);
		AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);

		// 动画集
		AnimationSet set1 = new AnimationSet(true);
		set1.addAnimation(tAnimation);
		set1.addAnimation(alphaAnimation);
		set1.setDuration(duration);
		set1.setInterpolator(new DecelerateInterpolator());
		rl_reading.startAnimation(set1);
		set1.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				rl_reading.setVisibility(View.VISIBLE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
//				read_line.setVisibility(View.VISIBLE);
				startWarnAnimation();
			}
		});
	}


	private void startWarnAnimation() {
		TranslateAnimation tAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,
				Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -0.3f, Animation.RELATIVE_TO_SELF, 0f);
		AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);

		// 动画集
		AnimationSet set1 = new AnimationSet(true);
		set1.addAnimation(tAnimation);
		set1.addAnimation(alphaAnimation);
		set1.setDuration(800);
		set1.setInterpolator(new DecelerateInterpolator());
		rl_dayAndWarn.startAnimation(set1);
		set1.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				rl_dayAndWarn.setVisibility(View.VISIBLE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				startHeadAnimation();
			}
		});
	}

	private void startHeadAnimation() {
		AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);
		alphaAnimation.setDuration(duration * 2);
		iv_gerenzhuye.startAnimation(alphaAnimation);
		alphaAnimation.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
				iv_gerenzhuye.setVisibility(View.VISIBLE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
			}
		});
	}
	
	
	private void getWordIsUpdate() {
		if (!NetworkUtil.isNetworkAvailable(this)) {
			AppUtil.showToast(this, R.string.no_network);
			return;
		}
		mWordTask.getWordIsUpdate(WordHomeDataUtil.getWordUpdateTime(this), "check", new HttpRequestAbstractCallBack(this,false) {
			@Override
			public void onStartCallBack() {
				super.onStartCallBack();
				AppUtil.showProgressDialog(mContext);
			}

			@Override
			public void onSuccessCallBack(String result) {
				super.onSuccessCallBack(result);
				LogUtils.d("0000000000"+result);
				AppUtil.dismissProgressDialog();
				try {
					WordNewCheckResponse wordNewCheckResponse = JSON.parseObject(result, WordNewCheckResponse.class);
					if (wordNewCheckResponse != null && 200 == wordNewCheckResponse.getStatus_code()) {
						WordNewCheckData data = wordNewCheckResponse.getData();
						if (data != null) {
							int state = data.getState();
							if (state == 1) {
								// 有更新了
								WordHomeDataUtil.putWordHaveUpdate(mContext, true);
								// 显示对话框
								showWordUpdateDialog();
							} else {
								WordHomeDataUtil.putWordHaveUpdate(mContext, false);
							}
						}
					}
				} catch (Exception e) {
					LogUtils.d("json数据异常");
					LogUtils.exception(e);
				}
			}

			@Override
			public void onFailureCallBack(HttpException error, String msg) {
				super.onFailureCallBack(error, msg);
				LogUtils.e(msg);
				AppUtil.dismissProgressDialog();
				AppUtil.showToast(mContext, R.string.load_net_data_failure);
			}
		});

	}
	
	private void showWordUpdateDialog() {
		// 有单词更新
		// 有复习计划未完成
		View view = LayoutInflater.from(this).inflate(R.layout.layout_rob_warn_dialog, null);

		final Dialog dialog = new Dialog(this, R.style.customDialog);
		dialog.setCanceledOnTouchOutside(false);
		dialog.setContentView(view);
		dialog.show();

		TextView tv_content = (TextView) view.findViewById(R.id.tv_content);
		tv_content.setText(R.string.word_practice_have_update);
		LinearLayout ll_layout1 = (LinearLayout) view.findViewById(R.id.ll_layout1);
		LinearLayout ll_layout2 = (LinearLayout) view.findViewById(R.id.ll_layout2);
		ll_layout1.setVisibility(View.VISIBLE);
		ll_layout2.setVisibility(View.GONE);

		Button btn_yes = (Button) view.findViewById(R.id.btn_yes);
		Button btn_no = (Button) view.findViewById(R.id.btn_no);
		btn_no.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (dialog != null && dialog.isShowing()) {
					dialog.cancel();
				}
			}
		});
		btn_yes.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (dialog != null && dialog.isShowing()) {
					dialog.cancel();
					// 离线管理
					Intent intent = new Intent(mContext, OfflineManageActivity.class);
					startActivity(intent);
				}

			}
		});

	}

}
